
year_strings = [
    '185001010000-186001010000',
    '186001010600-187001010000',
    '187001010600-188001010000',
    '188001010600-189001010000',
    '189001010600-190001010000',
    '190001010600-191001010000',
    '191001010600-192001010000',
    '192001010600-193001010000',
    '193001010600-194001010000',
    '194001020000-195001010000',
    '195001010600-196001010000',
    '196001010600-197001010000',
    '197001010600-198001010000',
    '198001010600-199001010000',
    '199001010600-200001010000',
    '200001010600-201001010000',
    '201001010600-201501010000'
]

print(config)

rule download:
    output:
          "{dataset}/raw/{name}/{name}_{year_str}_raw.nc",
    shell:
         "wget {config[server_prefix]}/AS-RCEC/TaiESM1/historical/{config["
         "run]}/6hrPlevPt/"
         "{config[cmip_name]}/gn/v20201112/"
         "{config[cmip_name]}_6hrPlevPt_TaiESM1_historical_{config[run]}_gn_{wildcards.year_str}.nc "
         "-O {wildcards.dataset}/raw/{config[name]}/{config[name]}_{wildcards.year_str}_raw.nc"
         
      #    http://esgf.rcec.sinica.edu.tw/thredds/fileServer/my_cmip6_dataroot/CMIP/AS-RCEC/TaiESM1/historical/r1i1p1f1/6hrPlevPt/ta/gn/v20201112/ta_6hrPlevPt_TaiESM1_historical_r1i1p1f1_gn_185001010000-186001010000.nc
         
      #    https://esgf.ceda.ac.uk/thredds/fileServer/esg_cmip6/CMIP6/CMIP/AS-RCEC/TaiESM1/historical/r1i1p1f1/6hrPlevPt/ta/gn/v20201112/ta_6hrPlevPt_TaiESM1_historical_r1i1p1f1_gn_185001010000-186001010000.nc

rule regrid:
    input:
          "{dataset}/raw/{name}/{name}_{year_str}_raw.nc"
    output:
          "{dataset}/{res}deg/{name}/{name}_{year_str}_{res}deg.nc.tmp"
    shell:
          "python ../../src/data_preprocessing/regrid.py \
            --input_fns {input} \
            --output_dir {wildcards.dataset}/{wildcards.res}deg/{wildcards.name} \
            --ddeg_out {wildcards.res} \
            --cmip 1 \
            --rename {config[cmip_name]} {config[era_name]} \
            --file_ending nc.tmp"

rule delete:
    input:
          expand("{{dataset}}/{res}deg/{{name}}/{{name}}_{{year_str}}_{res}deg.nc.tmp",
                 res=config['res']),
    output:
          expand("{{dataset}}/{res}deg/{{name}}/{{name}}_{{year_str}}_{res}deg.nc",
                 res=config['res'])
    priority: 100
    run:
          for i, o in zip(input, output):
              shell("mv {i} {o}")
      #     shell("rm {wildcards.dataset}/raw/{wildcards.name}/{wildcards.name}_{wildcards.year_str}_raw.nc"),


rule all:
    input:
         expand("{datadir}/{res}deg/{name}/{name}_{year_str}_{res}deg.nc",
                datadir=config['datadir'], res=config['res'], name=config['name'], year_str=year_strings)


